package com.hzw.saas.service.workspace.service;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzw.saas.api.workspace.IFileManagerService;
import com.hzw.saas.api.workspace.IWorkspaceResourceService;
import com.hzw.saas.api.workspace.bo.UserWorkspaceFileBo;
import com.hzw.saas.common.config.util.AssertUtil;
import com.hzw.saas.common.util.HttpContextUtils;
import com.hzw.saas.service.storage.config.StorageConfig;
import cn.hutool.core.io.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import jodd.io.FileNameUtil;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 工作空间资源管理业务实现
 * </p>
 *
 * @author sonam
 * @since 2021/2/3 1:10
 */
@AllArgsConstructor
@Service
@Slf4j
public class UserWorkspaceFileManagerServiceImpl implements IWorkspaceResourceService {

    private final StorageConfig mountConfig;
    private final IFileManagerService fileManagerService;

    private final HttpServletRequest request;
    private final HttpServletResponse response;

    @Override
    public String dealPath(String userId, String path) {
        AssertUtil.assertThrow("路径不能为空", StringUtil.isBlank(path));

        File mountPath = FileUtil.file(mountConfig.getWorkSpaceCatalog(), userId);
        if (!mountPath.exists()) {
            FileUtil.mkdir(mountPath);
        }
        String resultPath = FileUtil.file(mountPath, path).getAbsolutePath();

        log.debug("dealPath: " + resultPath);
        return resultPath;
    }

    @Override
    public File createDir(String userId, String dirPath) {
        dirPath = dealPath(userId, dirPath);
        return fileManagerService.createDir(dirPath);
    }

    @Override
    public void remove(String userId, String dirPath, boolean rf) {
        dirPath = dealPath(userId, dirPath);
        fileManagerService.removeDir(dirPath, rf);
    }

    @Override
    public File rename(String userId, String path, String name) {
        path = dealPath(userId, path);
        return fileManagerService.rename(path, name);
    }

    @Override
    public void move(String userId, String path, String targetDir) {
        path = dealPath(userId, path);
        targetDir = dealPath(userId, targetDir);
        fileManagerService.move(path, targetDir);
    }

    @Override
    public void copy(String userId, String path, String targetPath) {
        path = dealPath(userId, path);
        targetPath = dealPath(userId, targetPath);
        fileManagerService.copy(path, targetPath);
    }

    @Override
    public long countFiles(String userId) {
        if (StringUtil.isBlank(userId)) {
            return 0;
        }
        String dirPath = dealPath(userId, "/");
        return fileManagerService.getFileCount(dirPath);
    }

    @Override
    public long getFileSize(String userId, String dirPath) {
        dirPath = dealPath(userId, dirPath);
        return fileManagerService.getFileSize(dirPath);
    }

    @Override
    public List<UserWorkspaceFileBo> getFileList(String userId, String path) {
        path = dealPath(userId, path);
        return fileManagerService.getFileList(path);
    }

    @Override
    public void uploadFile(String userId, MultipartFile file, String path) {
        path = dealPath(userId, path);
        fileManagerService.uploadFile(file, path);
    }

    @Override
    public void downloadFile(String userId, String filepath) {
        filepath = dealPath(userId, filepath);
        try {
            HttpContextUtils.setFileDownloadHeader(request, response,
                FileNameUtil.getName(filepath),
                FileUtil.file(filepath).length());
            fileManagerService.downloadFile(filepath, response.getOutputStream());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
